package com.newasia.management.controller;

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.ManagementOrderService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.order.*;
import com.newasia.management.form.OrderDetail;
import com.newasia.management.form.OrderForm;
import com.newasia.management.form.PageInfo;
import com.newasia.management.form.PageResponse;
import com.newasia.management.shiro.ShiroUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * 腾讯课堂搜索【咕泡学院】
 * 官网：www.gupaoedu.com
 * 风骚的Mic 老师
 * create-date: 2019/7/30-上午9:26
 */
@RestController
@RequestMapping("/management")
public class OrderController {

    @Reference(timeout = 3000)
    ManagementOrderService managementOrderService;

    /**
     * 获取当前用户的所有订单
     * @return
     */
    @GetMapping("/order")
    public ResponseData orderByCurrentId(PageInfo pageInfo){
        OrderListRequest request = new OrderListRequest();
        request.setPage(pageInfo.getPage());
        request.setSize(pageInfo.getSize());
        request.setSort(pageInfo.getSort());
        request.setStatus(pageInfo.getStatus());
        request.setKey(pageInfo.getKey());
        Long uid = ShiroUtils.getUserInfo().getUserId();
        request.setUserId(uid);
        OrderListResponse listResponse = managementOrderService.orderList(request);
        if(listResponse.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            PageResponse response = new PageResponse();
            response.setData(listResponse.getDetailInfoList());
            response.setTotal(listResponse.getTotal());
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(listResponse);
    }

    /**
     * 查询订单详情
     * @return
     */
    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable String id){
        OrderDetailRequest request=new OrderDetailRequest();
        request.setOrderId(id);
        OrderDetailResponse response=managementOrderService.orderDetail(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response);
    }

    @PutMapping("/order")
    public ResponseData orderUpdate(@RequestBody OrderForm form){
        UpdateOrderRequest request = new UpdateOrderRequest();
        request.setOrderId(form.getOrderId());
        request.setPaymentType(form.getPaymentType());
        request.setPostFee(form.getPostFee());
        request.setStatus(form.getStatus());
        request.setPaymentTime(form.getPaymentTime());
        request.setConsignTime(form.getConsignTime());
        request.setEndTime(form.getEndTime());
        request.setCloseTime(form.getCloseTime());
        request.setShippingName(form.getShippingName());
        request.setShippingCode(form.getShippingCode());
        request.setBuyerNick(form.getBuyerNick());
        request.setBuyerMessage(form.getBuyerMessage());

        UpdateOrderResponse response = managementOrderService.updateOrder(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response);
    }

    /*@GetMapping("/order")
    public ResponseData orderCancel(PageInfo pageInfo,HttpServletRequest servletRequest){
        pageInfo.getPage();
        pageInfo.getSize();
        pageInfo.getSort();
        pageInfo.getStatus();
        pageInfo.getKey();
        return new ResponseUtil<>().setData(null);
    }*/

    /**
     * 取消订单
     * @return
     */
    /*@GetMapping("/order")
    public ResponseData orderCancel(PageInfo pageInfo,HttpServletRequest servletRequest){
        pageInfo.getPage();
        pageInfo.getSize();
        pageInfo.getSort();
        pageInfo.getStatus();
        pageInfo.getKey();
        return new ResponseUtil<>().setData(null);
    }*/

    /**
     * 删除订单
     * @param id
     * @return
     */
    /*@PutMapping("/order/{id}")
    public ResponseData orderDel(@PathVariable String id){
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }*/

}
